"""
和文件相关的类
"""
from data_define import Record
class FileReader:
    def __init__(self, filename):
        self.filename = filename

    def read(self)->list[Record]:
        raise NotImplementedError("子类必须实现这个方法")
class TextReader(FileReader):
    def read(self)->list[Record]:
        records = []
        with open(self.filename, 'r', encoding='utf-8') as file:
            for line in file:
                date, order_id, amount, province = line.strip().split(',')
                record = Record(date, order_id, float(amount), province)
                records.append(record)
        return records
class JSONReader(FileReader):
    def read(self)->list[Record]:
        import json
        records = []
        with open(self.filename, 'r', encoding='utf-8') as file:
            for line in file:
                item = json.loads(line)
                record = Record(item['date'], item['order_id'], float(item['money']), item['province'])
                records.append(record)
        return records

if __name__ == '__main__':
    text_reader = TextReader('2011年1月销售数据.txt')
    text_records = text_reader.read()
    for record in text_records:
        print(record)

    json_reader = JSONReader('2011年2月销售数据JSON.txt')
    json_records = json_reader.read()
    for record in json_records:
        print(record)